package com.qqtech.qquan.user.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.sys.enums.SysConst;
import com.qqtech.qquan.sys.model.SysParam;
import com.qqtech.qquan.sys.service.SysParamService;
import com.qqtech.qquan.user.model.UserMoneyLog;
import com.qqtech.qquan.user.model.UserMoneyLogQuery;
import com.qqtech.qquan.user.model.UserMoneyLogVo;
import com.qqtech.qquan.user.service.UserMoneyLogService;
import com.qqtech.qquan.user.service.UserSysDictService;
import com.qqtech.qquan.util.ExcelUtil;

@Controller
@RequestMapping(value = "/user/money/log")
public class UserMoneyLogController extends BaseAdminController {
	@Resource
	private UserMoneyLogService userMoneyLogService;
	@Resource
	private SysParamService sysParamService;
	@Resource
	private UserSysDictService userSysDictService;
	
	@Override
	protected BaseService<UserMoneyLog> getBaseService() {
		return userMoneyLogService;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/user/moneyLog/moneyLog-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(UserMoneyLogQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<UserMoneyLogVo> page = userMoneyLogService.queryPageListAdmin(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	@RequestMapping(value = "/userMoneyList", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage userMoneyList(UserMoneyLogQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<UserMoneyLogVo> page = userMoneyLogService.queryPageListAdmin(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	@RequestMapping(value = "/userMoneyList/{userId}", method = RequestMethod.GET)
	public String userMoneyList(@PathVariable Integer userId) {
		return "/user/moneyLog/userMoneyLog-list";
	}
	
	/**
	 * @param query
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/export2Excel")
	@ResponseBody
	public Result export2Excel(UserMoneyLogQuery query, HttpServletRequest request, HttpServletResponse response) {
		Result ret = new Result();
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.DESC, "newAt"));
		int count = userMoneyLogService.queryCount(query);
		SysParam sysParam = sysParamService.getByKey(SysConst.PARAM_SYS_EXPORT_TOEXCEL_MAXROWS);
		if (sysParam != null && StringUtil.isNotBlank(sysParam.getValue())) {
			int maxExportRows = Integer.valueOf(sysParam.getValue());
			if (count > maxExportRows) {
				ret.addErr("导出数量不能超过" + maxExportRows + "行!");
				return ret;
			}
		}
		List<UserMoneyLogVo> moneyLogList = userMoneyLogService.queryListAdmin(query, pageKit);// 不分页
		List<Map<String, Object>> list = createExcelRecord(moneyLogList);
		String columnNames[] = { "昵称", "交易额(正数为入账,负数为出账)", "(交易后)余额", "交易备注", "交易类型", "时间"};// 列名
		String keys[] = { "userNick", "money", "balance", "remark", "type", "strNewAt"};// map中的key
		String fileName = "交易流水";
		ExcelUtil.ExcelSingleOutputStream(list, keys, columnNames, fileName, request, response);
		return ret;
	}

	private List<Map<String, Object>> createExcelRecord(List<UserMoneyLogVo> moneyLogList) {
		List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sheetName", "交易流水");
		listmap.add(map);
		UserMoneyLogVo moneyLogVo = null;
		
		for (int j = 0; j < moneyLogList.size(); j++) {
			moneyLogVo = moneyLogList.get(j);
			Map<String, Object> mapValue = new HashMap<String, Object>();
			mapValue.put("userNick", moneyLogVo.getUserNick());
			mapValue.put("money", moneyLogVo.getMoney());
			mapValue.put("balance", moneyLogVo.getBalance());
			mapValue.put("remark", moneyLogVo.getRemark());
			mapValue.put("type", moneyLogVo.getType()==1 ? "圈币" : "余额");
			mapValue.put("strNewAt", moneyLogVo.getStrNewAt());
			listmap.add(mapValue);
		}
		return listmap;
	}
	
	/**
	 * 导出一个用户的交易流水
	 * @param query
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/export2Excel4User")
	@ResponseBody
	public Result export2Excel4User(UserMoneyLogQuery query, HttpServletRequest request, HttpServletResponse response) {
		Result ret = new Result();
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.DESC, "newAt"));
		int count = userMoneyLogService.queryCount(query);
		SysParam sysParam = sysParamService.getByKey(SysConst.PARAM_SYS_EXPORT_TOEXCEL_MAXROWS);
		if (sysParam != null && StringUtil.isNotBlank(sysParam.getValue())) {
			int maxExportRows = Integer.valueOf(sysParam.getValue());
			if (count > maxExportRows) {
				ret.addErr("导出数量不能超过" + maxExportRows + "行!");
				return ret;
			}
		}
		List<UserMoneyLogVo> moneyLogList = userMoneyLogService.queryListAdmin(query, pageKit);// 不分页
		List<Map<String, Object>> list = createExcelRecord(moneyLogList);
		String columnNames[] = { "昵称", "交易额(正数为入账,负数为出账)", "(交易后)余额", "交易备注", "交易类型", "时间"};// 列名
		String keys[] = { "userNick", "money", "balance", "remark", "type", "strNewAt"};// map中的key
		String fileName = "用户-交易流水";
		ExcelUtil.ExcelSingleOutputStream(list, keys, columnNames, fileName, request, response);
		return ret;
	}

}
